<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Aug 13, 2013
 * Time: 11:45:17 AM
 * To change this template use File | Settings | File Templates.
 */
require('libraries/fpdf17/fpdf.php');
class PrintDistribution extends FPDF {
    private $db;

    public function __construct($orientation = 'P', $unit = 'mm', $size = 'Letter') {
        parent::__construct($orientation, $unit, $size);
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;

    }


    // Page header
    function Header() {
        // Logo
        $this->Image('images/app/logo_icon.png', 22, 17, 16);
        $this->SetFont('Arial', 'B', 8);
        $this->Cell(20, 22, '', 1, 0, 'C');
        $this->SetFont('Arial', 'B', 15);
        $this->Cell(160, 8, 'Personal Monitoring Service ', 'LTR', 2, 'L');
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(160, 4, 'General Scientific Division, Atomic Energy Authority', 'LR', 2, 'L');
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(160, 5, 'No.60/460, Baseline Road, Orugodawatta, Wellampitiya', 'LR', 2, 'L');
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(160, 5, 'Tel:94-11-2533427-8,2533449 Fax:94-11-2533448  E-mail: pms@aea.gov.lk', 'LRB', 2, 'L');
        $this->Ln();
        $this->SetFont('Arial', 'B', 8);
        $this->Cell(2, -15, '', 0, 0, 'L');
        $this->Cell(2, -15, 'PMS-AEA', 0, 0, 'L');

        // Line break
        $this->Ln(0);
    }

// Page footer
    function Footer() {
        // Position at 1.5 cm from bottom
        $this->SetFont('Arial', 'I', 8);
        $this->SetY(-23);
        $this->Cell(180, 5, 'Official Use Only', 1, 1, 'L');
        $this->Cell(60, 4, 'Date of Reccept:', 1, 0, 'L');
        $this->Cell(60, 4, 'Date of Processed:', 1, 0, 'L');
        $this->Cell(60, 4, 'Date of Print:', 1, 1, 'L');
        // Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        // Page number
        $this->Cell(0, 5, 'Page ' . $this->PageNo() . 'of {nb}', 0, 0, 'R');
    }

    // Simple table
    function BasicTable($insId, $secId, $fromMonth, $fromYear, $toMonth, $toYear) {

        //calculate period of the distribution
        $_fromDate = $fromYear . "-" . $fromMonth . "-01";
        $_toDateTemp = $toYear . "-" . $toMonth . "-01";
        $_toMonth = new DateTime($_toDateTemp);
        $_lastDay = $_toMonth->format('t');

        $fromDateTime = new DateTime($_fromDate);
        $fromDateMonth = $fromDateTime->format('F');
        $toDateMonth = $_toMonth->format('F');
        $_toDate = $toYear . "-" . $toMonth . "-" . $_lastDay;

        if ($secId == 0) {
            $sql = "SELECT * FROM aea_institute WHERE id='$insId' LIMIT 1";
        } else {
            $sql = "SELECT * FROM aea_institute WHERE id='$secId' LIMIT 1";
        }
        $insRecord = $this->db->Execute($sql);


        $sql = " SELECT aea_distribution_card.id AS ID,`employ_id`,`card_id`,from_date,to_date,`received_date`,full_name,init_name,title FROM `aea_distribution_card`
                    LEFT JOIN aea_profile on aea_distribution_card.employ_id = aea_profile.id
                    WHERE `list_id`=(SELECT id from aea_distribution_list
                                        WHERE  institute_id='$insId'
                                        AND section_id='$secId'
                                        AND month='$fromMonth'
                                        AND year='$fromYear'
                                        AND to_month='$toMonth'
                                        AND to_year='$toYear')";
        $resAll = $this->db->Execute($sql);
        $totalRows = $resAll->RecordCount();
        if ($totalRows > 0) {
            $this->SetFont('Arial', 'B', 12);
            $this->Cell(140, 8, 'DISTRIBUTION OF TL DOSIMETERS ', 0, 2, 'C');
            $this->SetFont('Arial', '', 10);
            $this->Cell(50, 5, 'NAME OF THE RECEIVER ', 0, 0, 'L');
            $this->Cell(2, 5, ':', 0, 0, 'L');
            $this->Cell(50, 5, $insRecord->fields['head_name'], 0, 1, 'L');
            $this->Cell(50, 5, 'INSTITUTION & ADDRESS ', 0, 0, 'L');
            $this->Cell(2, 5, ':', 0, 0, 'L');
            $this->MultiCell(100, 5, $insRecord->fields['address'], 0, 'L');
            $this->Cell(50, 5, 'INSTITUTE NUMBER ', 0, 0, 'L');
            $this->Cell(2, 5, ':', 0, 0, 'L');
            $this->Cell(50, 5, "PMS/" . $insRecord->fields['id'], 0, 1, 'L');
            $this->Cell(50, 5, 'MONITORING  PERIOD ', 0, 0, 'L');
            $this->Cell(2, 5, ':', 0, 0, 'L');
            $this->Cell(50, 5, $_fromDate . " To " . $_toDate . " ( " . $fromDateMonth . " / " . $toDateMonth . " " . $toYear . " )", 0, 1, 'L');
            $this->Cell(50, 5, 'ATTENTION', 0, 0, 'L');
            $this->Cell(2, 5, ':', 0, 0, 'L');
            $this->Cell(50, 5, $insRecord->fields['rpo_name'], 0, 1, 'L');
            $this->Ln();
            $w = array(12, 75, 15, 20, 20, 20, 20);
            $this->SetFont('Arial', 'B', 8);
            $this->Cell($w[0], 10, "Ref.Num", 1, 0, 'C');
            $this->Cell($w[1], 10, "Empoyee Name", 1, 0, 'C');
            $this->MultiCell($w[2], 5, "TLD Card\n Number", 1, 'C');
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetXY($x + 102, $y - 10);
            $this->MultiCell($w[3], 5, "Whether Used or Not", 1, 'L');
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetXY($x + 122, $y - 10);
            $this->Cell($w[4] + $w[5] + $w[6], 5, "For Official Use", 1, 1, 'C');
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetXY($x + 122, $y);
            $this->SetFont('Arial', 'B', 7);
            $this->Cell($w[4], 5, "Reading ii(uSv)", 1, 0, 'C');
            $this->Cell($w[5], 5, "Reading iii(uSv)", 1, 0, 'C');
            $this->Cell($w[6], 5, "DOSE(uSv)", 1, 1, 'C');
            $i = 1;
            $this->SetFont('Arial', '', 8);
            while (!$resAll->EOF) {

                $this->Cell($w[0], 6, $i, 'LRB', 0, 'L');
                if ($resAll->fields['employ_id'] == 0) {
                    $this->Cell($w[1], 6, "Background Card", 'RB', 0, 'L');
                } else {
                    $this->Cell($w[1], 6, $resAll->fields['title'] . " " . $resAll->fields['init_name'], 'RB', 0, 'L');
                }
                $this->Cell($w[2], 6, $resAll->fields['card_id'], 'LB', 0, 'R');
                $this->Cell($w[3], 6, '', 'LRB', 0, 'R');
                $this->Cell($w[4], 6, '', 'LRB', 0, 'R');
                $this->Cell($w[5], 6, '', 'LRB', 0, 'R');
                $this->Cell($w[6], 6, '', 'LRB', 0, 'R');
                $this->Ln();
                $i++;
                $resAll->MoveNext();
            }
            // Closing line
            $this->Cell(array_sum($w), 0, '', 'T');
            $this->Ln();
            $fill = true;
            //    $this->SetFillColor(100,149,237);
            $this->Cell($w[0], 6, '', 'LB', 0, 'L');
            $this->SetFont('Arial', 'B', 8);
            $this->Cell($w[1], 6, 'Total Number of TLD Cards', 'RB', 0, 'L');
            $this->Cell($w[2], 6, --$i, 'LRB', 0, 'R');
            //  $this->Cell($w[3],6,'','LRBT',0,'R');
            $this->Ln();
            $this->Ln();
            $this->SetFont('Arial', 'B', 9);
            $this->Cell(140, 6, 'Please read instructions given below & instructions provided to Radiation Protection Officer (PMS/INS-02).', 0, 0, 'L');

            $this->Ln();
            $this->Ln();
            $this->SetFont('Arial', 'B', 10);
            $this->Cell(6, 4, '01. ', 0, 0, 'L');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, ' Used & unused TLD cards should be returned together with the duplicate of this form at least within one week on the receipt of  a fresh batch of TLD cards for next monitoring period, either by registered post or by hand  to the address given on the top of this form . Please indicate');
            $this->SetFont('Arial', 'BU', 10);
            $this->Write(4, ' "Personal Monitoring Service/TLD" ');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, 'on the top left hand corner of the envelope');
            $this->Ln();
            $this->Ln();

            $this->SetFont('Arial', 'B', 10);
            $this->Cell(6, 4, '02. ', 0, 0, 'L');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, ' If TLD cards are not used, please indicate by letter');
            $this->SetFont('Arial', 'B', 10);
            $this->Write(4, ' NU');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, '  in the column provided for. If the service is no longer required, please indicate by letters ');
            $this->SetFont('Arial', 'B', 10);
            $this->Write(4, ' NLR');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, '  in front of the names of such persons.');
            $this->Ln();
            $this->Ln();
            $this->SetFont('Arial', 'B', 10);
            $this->Cell(6, 4, '03. ', 0, 0, 'L');
            $this->SetFont('Arial', '', 10);
            $this->Write(4, ' Personnel who are no longer in service should be informed through Head of the Department /Institution in order to terminate service.');
            $this->Ln();
            $this->Ln();
            $this->SetFont('Arial', 'B', 10);
            $this->Cell(80, 5, 'No. of TLD cards sent by AEA:', 1, 0, 'L');
            $this->Cell(15, 5, $i, 1, 1, 'C');
            $this->Ln();
            $this->Cell(110, 5, 'Signature of Authorized Person:', 0, 0, 'L');
            $this->Cell(15, 5, 'Date :', 0, 1, 'C');
            $this->Ln();
            $this->Cell(80, 5, 'No. of TLD cards returned by the customer :', 1, 0, 'L');
            $this->Cell(15, 5, '', 1, 1, 'C');
            $this->Ln();
            $this->Cell(80, 5, 'Decleration:', 0, 1, 'L');
            $this->Write(4, 'I hereby certify that TLD cards with bearing nos. indicated against the names of workers given in the distribution form (Ref: PMS/DF) had correctly worn by the workers during the monitoring period mentioned above and information provided in this form is true and accurate. ');
            $this->Ln();
            $this->Ln();
            $this->Cell(110, 5, 'Name & Signature of the Authorized Person/RPO : ', 0, 1, 'L');
            $this->Cell(20, 5, 'Date:', 0, 1);            

        }


    }

// Better table
    function ImprovedTable($header, $data) {
        // Column widths
        $w = array(40, 35, 40, 45,);
        // Header
        for ($i = 0; $i < count($header); $i++)
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C');
        $this->Ln();
        // Data
        foreach ($data as $row)
        {
            $this->Cell($w[0], 6, $row[0], 'LR');
            $this->Cell($w[1], 6, $row[1], 'LR');
            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R');
            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R');
            $this->Ln();
        }
        // Closing line
        $this->Cell(array_sum($w), 0, '', 'T');
    }

// Colored table
    function FancyTable($header, $data) {
        // Colors, line width and bold font
        $this->SetFillColor(255, 0, 0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(.3);
        $this->SetFont('', 'B');
        // Header
        $w = array(40, 35, 40, 45);
        for ($i = 0; $i < count($header); $i++)
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true);
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
        // Data
        $fill = false;
        foreach ($data as $row)
        {
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
            $this->Ln();
            $fill = !$fill;
        }
        // Closing line
        $this->Cell(array_sum($w), 0, '', 'T');
    }

    function printDistributionList($insId, $secId, $fromMonth, $fromYear, $toMonth, $toYear) {
        $sql = " SELECT aea_distribution_card.id AS ID,`employ_id`,`card_id`,from_date,to_date,`received_date`,full_name FROM `aea_distribution_card`
                    LEFT JOIN aea_profile on aea_distribution_card.employ_id = aea_profile.id
                    WHERE `list_id`=(SELECT id from aea_distribution_list
                                        WHERE  institute_id='$insId'
                                        AND section_id='$secId'
                                        AND month='$fromMonth'
                                        AND year='$fromYear'
                                        AND to_month='$toMonth'
                                        AND to_year='$toYear')";
        $resAll = $this->db->Execute($sql);
        $totalRows = $resAll->RecordCount();
        if ($totalRows > 0) {
            $this->SetFont('Arial', 'B', 12);
            $this->Cell(140, 8, 'DISTRIBUTION OF TL DOSIMETERS ', 0, 2, 'C');
            $this->SetFont('Arial', '', 10);
            $this->Cell(50, 5, 'NAME OF THE RECEIVER ', 0, 0, 'L');
            $this->Cell(50, 5, ': HEAD NAME', 0, 1, 'L');
            $this->Cell(50, 5, 'INSTITUTION & ADDRESS ', 0, 0, 'L');
            $this->Cell(50, 5, ': aDDRESS', 0, 1, 'L');
            $this->Cell(50, 5, 'INSTITUTE NUMBER ', 0, 0, 'L');
            $this->Cell(50, 5, ': PMS/20', 0, 1, 'L');
            $this->Cell(50, 5, 'MONITORING  PERIOD ', 0, 0, 'L');
            $this->Cell(50, 5, ':  01.09.2013 TO 31.10.2013  (SEPTEMBER /OCTOBER 2013)', 0, 1, 'L');
            $this->Cell(50, 5, 'ATTENTION', 0, 0, 'L');
            $this->Cell(50, 5, ':  RADIATION PROTECTION OFFICER ', 0, 1, 'L');
            $this->SetFillColor(0, 206, 209);
            $this->SetTextColor(0, 0, 0);
            $this->SetDrawColor(66, 66, 111);
            $this->SetLineWidth(.3);
            $this->SetFont('', 'B', 6);
            // Header
            $w = array(10, 80, 20, 20);
            $this->Cell($w[0], 10, "", 1, 0, 'C', true);
            $this->Cell($w[1], 10, "NAME", 1, 0, 'C', true);
            $this->MultiCell($w[2], 5, "TLD CARD\n NUMBER", 1, 'C', true);
            $x = $this->GetX();
            $y = $this->GetY();
            $this->SetXY($x + 110, $y - 10);
            $this->MultiCell($w[3], 5, "WHETHER\n USED OR NOT", 1, 'L', true);
            // $this->Ln();
            // Color and font restoration
            $this->SetFillColor(224, 235, 255);
            $this->SetTextColor(0);
            $this->SetFont('', '', 10);
            // Data
            $fill = false;
            $i = 1;
            while (!$resAll->EOF) {
                $this->Cell($w[0], 6, $i, 'LR', 0, 'L', $fill);
                if ($resAll->fields['employ_id'] == 0) {
                    $this->Cell($w[1], 6, "Background Card", 'LR', 0, 'L', $fill);
                } else {
                    $this->Cell($w[1], 6, $resAll->fields['full_name'], 'LR', 0, 'L', $fill);
                }

                $this->Cell($w[2], 6, $resAll->fields['card_id'], 'LR', 0, 'R', $fill);
                $this->Cell($w[2], 6, '', 'LR', 0, 'R', $fill);
                $this->Ln();
                $fill = !$fill;
                $i++;
                $resAll->MoveNext();
            }
            // Closing line
            $this->Cell(array_sum($w), 0, '', 'T');
            $this->Ln();
            $fill = true;
            $this->SetFillColor(100, 149, 237);
            $this->Cell($w[0], 6, '', 'LT', 0, 'L', $fill);
            $this->Cell($w[1], 6, 'Total Number of Card', 'RT', 0, 'L', $fill);
            $this->Cell($w[2], 6, --$i, 'LRT', 0, 'R', $fill);
            $this->Ln();
            $this->Cell(array_sum($w), 0, '', 'T');
        }
    }

    function checkResult($insId, $secId, $fromMonth, $fromYear, $toMonth, $toYear) {
        $sql = " SELECT aea_distribution_card.id AS ID,`employ_id`,`card_id`,from_date,to_date,`received_date`,full_name FROM `aea_distribution_card`
                    LEFT JOIN aea_profile on aea_distribution_card.employ_id = aea_profile.id
                    WHERE `list_id`=(SELECT id from aea_distribution_list
                                        WHERE  institute_id='$insId'
                                        AND section_id='$secId'
                                        AND month='$fromMonth'
                                        AND year='$fromYear'
                                        AND to_month='$toMonth'
                                        AND to_year='$toYear')";
        $resAll = $this->db->Execute($sql);
        $totalRows = $resAll->RecordCount();
        if ($totalRows > 0) {
            return true;
        } else {
            return false;
        }

    }


}
